"""
Case Type   : checksum
Case Name   : char_length(str)函数参数类型为:text类型
Create At   : 2024.09.23
Owner       : @zhanghuan96
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、客户端编码为gbk，查询字符长度
    3、客户端编码为sql_ascii，查询字符长度
    4、清理环境
Expect      :
    1、成功，插件查询成功
    2、纯汉字报错，其余成功
    3、成功
    4、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_char_0007'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = (f"drop database if exists {self.db_name};"
                   f"create database {self.db_name}  dbcompatibility ='B';")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(1,
                         sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                         '兼容B库创建失败')
        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '---step2:客户端编码为gbk，查询字符长度;expect:纯汉字报错，其余成功---'
        self.log.info(text)
        sql_cmd2 = ("set client_encoding = 'gbk';"
                    "show client_encoding;"
                    "select char_length(166235);"
                    "select char_length('268568');"
                    "select char_length('qweqwer');"
                    "select char_length('vfgdvfxf');"
                    "select char_length(b'111000101');"
                    "select char_length('@!#@#$#%$');"
                    "select char_length('哈哈哈dasr@#@%$#qwe1563165');")
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        res_char = sql_res2.splitlines()
        self.log.info(res_char)
        self.assertIn('SET', sql_res2, '创建失败')
        self.assertEqual('6', sql_res2.splitlines()[8].strip(),
                         '创建失败')
        self.assertEqual('6', sql_res2.splitlines()[13].strip(),
                         '创建失败')
        self.assertEqual('7', sql_res2.splitlines()[18].strip(),
                         '创建失败')
        self.assertEqual('8', sql_res2.splitlines()[23].strip(),
                         '创建失败')
        self.assertEqual('2', sql_res2.splitlines()[28].strip(),
                         '创建失败')
        self.assertEqual('8', sql_res2.splitlines()[33].strip(),
                         '创建失败')
        self.assertEqual('22', sql_res2.splitlines()[-2].strip(),
                         '创建失败')
        sql_cmd = ("set client_encoding = 'gbk';"
                   "select char_length('画个图已发送冬天热');")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('SET', sql_res, '创建失败')
        self.assertIn('ERROR', sql_res, '创建失败')

        text = '-step3:客户端编码为sql_ascii，查询字符长度;expect:成功-'
        self.log.info(text)
        sql_cmd3 = ("set client_encoding = 'sql_ascii';"
                    "show client_encoding;"
                    "select char_length(166235);"
                    "select char_length('268568');"
                    "select char_length('qweqwer');"
                    "select char_length('vfgdvfxf');"
                    "select char_length(b'111000101');"
                    "select char_length('@!#@#$#%$');"
                    "select char_length('画个图已发送冬天热');"
                    "select char_length('哈哈哈dasr@#@%$#qwe1563165');")
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3,
                                                 dbname=self.db_name)
        self.log.info(sql_res3)
        self.assertIn('SET', sql_res3, '创建失败')
        res_char3 = sql_res3.splitlines()
        self.log.info(res_char3)
        self.assertEqual('6', sql_res3.splitlines()[8].strip(),
                         '创建失败')
        self.assertEqual('6', sql_res3.splitlines()[13].strip(),
                         '创建失败')
        self.assertEqual('7', sql_res3.splitlines()[18].strip(),
                         '创建失败')
        self.assertEqual('8', sql_res3.splitlines()[23].strip(),
                         '创建失败')
        self.assertEqual('2', sql_res3.splitlines()[28].strip(),
                         '创建失败')
        self.assertEqual('8', sql_res3.splitlines()[33].strip(),
                         '创建失败')
        self.assertEqual('9', sql_res3.splitlines()[38].strip(),
                         '创建失败')
        self.assertEqual('22', sql_res3.splitlines()[-2].strip(),
                         '创建失败')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4, dbname='postgres')
        self.log.info(sql_res4)
        self.assertEqual(1,
                         sql_res4.count(self.constant.DROP_DATABASE_SUCCESS),
                         '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
